//
//  StoreViewController.m
//  BeautyClother
//
//  Created by dllo on 15/8/21.
//  Copyright (c) 2015年 宋爽. All rights reserved.
//



#import "StoreViewController.h"
#import "SuitPullLayout.h"
#import "AsyncConnection.h"
#import "UIImageView+WebCache.h"
#import "define.h"
#import "SuitCell.h"
#import "StoreLayout.h"
#import "HeaderFooterView.h"
#import "MJRefresh.h"
#import "CollectionController.h"
#import "OneDetailTableViewCell.h"
#import "TopicDetailViewController.h"
#import "DataBase.h"
#import "ItemDataBase.h"
#import "ItemsModel.h"


#import "MBProgressHUD.h"
#import "Reachability.h"

NSInteger flag = 2;


@interface StoreViewController ()<UITableViewDataSource, UITableViewDelegate, UIScrollViewDelegate,UICollectionViewDataSource, UICollectionViewDelegate>

@property (nonatomic, retain) UIScrollView *scroll;
@property (nonatomic, retain) UICollectionView *collAmerica;
@property (nonatomic, retain) NSMutableArray *myArr;
@property (nonatomic, retain) NSMutableArray *array;
@property (nonatomic, retain) UIScrollView *bigScroll;
@property (nonatomic, retain) NSMutableArray *otherArr;
@property (nonatomic, retain) UICollectionView *collnew;
@property (nonatomic, retain) NSMutableArray *searchArr;
@property (nonatomic, retain) UITableView *table;
@property (nonatomic, retain) UITableView *left;
@property (nonatomic, retain) ItemsModel *model;
@property (nonatomic, retain) NSMutableArray *collArr;
@property (nonatomic, retain) NSMutableArray *otherArr1;
@property (nonatomic, retain) NSMutableArray *otherArr2;
@property (nonatomic, retain) UICollectionView *collKorea;
@property (nonatomic, retain) NSMutableArray *arr;
@property (nonatomic, retain) UITextField *navigationText;
@property (nonatomic, retain) StoreLayout *flow1;
@property (nonatomic, retain) StoreLayout *flow2;
@property (nonatomic, retain) StoreLayout *flow3;
@property (nonatomic, retain) MBProgressHUD *mb;
@property (nonatomic, copy) NSString *flag1;
@property (nonatomic, copy) NSString *flag2;
@property (nonatomic, copy) NSString *flag3;

@property (nonatomic, assign) CGFloat offWidth;
@property (nonatomic, assign) CGFloat offHeight;
@property (nonatomic, retain) NSDictionary *dic;

@end

@implementation StoreViewController

- (void)dealloc
{
    [_scroll release];
    [_collAmerica release];
    [_myArr release];
    [_array release];
    [_bigScroll release];
    [_otherArr release];
    [_collnew release];
    [_searchArr release];
    [_table release];
    [_left release];
    [_model release];
    [_collArr release];
    [_otherArr1 release];
    [_otherArr2 release];
    [_collKorea release];
    [_arr release];
    [_navigationText release];
    [_flow1 release];
    [_flow2 release];
    [_flow3 release];
    [_mb release];
    [_dic release];
    [super dealloc];
}

- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        
        
        
        self.array = [NSMutableArray array];
        
        self.myArr = @[@"最新", @"热门", @"欧美", @"日韩"].mutableCopy;
        self.searchArr = [NSMutableArray array];
        self.searchArr = @[@"百变 '范爷'时尚经典", @"三伏来袭,清爽无袖T恤", @"一双尖头鞋,提升时尚指数", @"最时尚!世界秀场聚集", @"炫酷夏日凉鞋怎么搭", @"夏日百搭短袖T恤", @"炎热夏日,人人必备牛仔短裤", @"早春街头范:破洞牛仔裤", @"春暖花开时,印花连衣裙", @"时尚必备手提包", @"皮裤打造时尚欧美范", @"青春的代言- 倪妮",@"新晋女神-After School", @"展现完美腿型-紧身裤", @"性感唇印放在身", @"怎么会不爱条纹", @"衣橱不可缺少的牛仔单品", @"常备百搭-单肩包", @"出行好伴侣-双肩包"].mutableCopy;
       
        self.model = [[ItemsModel alloc]init];
        self.collArr = [NSMutableArray array];
        self.otherArr = [NSMutableArray array];
        self.otherArr1 = [NSMutableArray array];
        self.otherArr2 = [NSMutableArray array];
        self.arr = [NSMutableArray array];
        self.arr = @[@"%E5%85%A8%E9%83%A8", @"热门榜", @"欧美", @"日韩", @"%E6%9C%AC%E5%9C%9F", @"%E5%9E%8B%E7%94%B7"].mutableCopy;
        self.offWidth = [[UIScreen mainScreen]bounds].size.width / 375;
        self.offHeight = [[UIScreen mainScreen]bounds].size.height / 667;
        
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    UILabel *title = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 100, 50)];
    title.font = [UIFont boldSystemFontOfSize:20];
    title.textColor = [UIColor colorWithRed:255/255 green:0 blue:255/255 alpha:0.8];
    title.textAlignment = NSTextAlignmentCenter;
    title.text = @"美美搭";
    self.navigationItem.titleView = title;
    [self hotConnection];
    [self createScrollView];
    [self createBigScroll];
    [self dataConnection];
    
    
    [self createNewCollection];
    [self createAcollection];
    [self createKcollection];
    [self addFooter];
    [self addHeader];
    
    self.automaticallyAdjustsScrollViewInsets = NO;
}




#pragma mark -
#pragma mark 上拉加载 下拉刷新

/// 加载刷新
- (void)addHeader
{
    __block StoreViewController *actionVC = self;
    
    [self.table addHeaderWithCallback:^{
        
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            
            [actionVC.table headerEndRefreshing];
           
        });
        [self.table headerEndRefreshing];
    }];

    [self.collnew addHeaderWithCallback:^{
        
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
    
            [actionVC.collnew headerEndRefreshing];
            if (self.otherArr.count != 0) {
                [self.collnew reloadData];
            }
           
        });
        [self.collnew headerEndRefreshing];
    }];

    
}
- (void)addFooter
{
    __block StoreViewController *actionVC = self;
    
    [self.table addFooterWithCallback:^{
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            
            NSDictionary *dic = @{@"token":@"1416b90f03d637a168f695105aa4132d7",
                                  @"page":[NSString stringWithFormat:@"%ld", (long)flag],
                                  @"ua":@"iPhone%7C1.7.11",
                                  @"lang":@"en",
                                  @"sig":@"34fcc5e66a371441154272890",
                                  @"nt":@"wifi"};
            [AsyncConnection asyncConnection:SUIT_HOT parmaters:dic block:^(NSData *data) {
                NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
                NSMutableArray *arr = [NSMutableArray array];
                arr = [dic objectForKey:@"topics"];
                [self.array addObjectsFromArray:arr];
               
               
                
                [self.table reloadData];
                flag = flag + 1;
            }path:@"suit_hot"];
            
            [actionVC.table footerEndRefreshing];
        });
    }];
    
    
    
    [self.collnew addFooterWithCallback:^{
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            
           
            NSDictionary *dic = @{@"category":@"热门榜",
                                  @"lts":@"",
                                  @"pin":@"",
                                  @"flag":self.flag1};
            [AsyncConnection asyncConnection:SUIT_OTHER parmaters:dic block:^(NSData *data) {
                NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
                NSMutableArray *arr = [NSMutableArray array];
                arr = [[dic objectForKey:@"data"]objectForKey:@"items"];
                
                [self.otherArr addObjectsFromArray:arr];
                 for (int i = 0; i < arr.count; i++) {
                ItemsModel *model = [[ItemsModel alloc]init];
                [model setValuesForKeysWithDictionary:[[self.otherArr objectAtIndex:i]objectForKey:@"component"]];
                model.height = [[[self.otherArr objectAtIndex:i]objectForKey:@"height"]floatValue];
                model.width = [[[self.otherArr objectAtIndex:i]objectForKey:@"width"]floatValue];
                model.desc = [[[self.otherArr objectAtIndex:i]objectForKey:@"component"]objectForKey:@"description"];
                model.iid = [[[self.otherArr objectAtIndex:i]objectForKey:@"component"]objectForKey:@"id"];
                [self.collArr addObject:model];
                 }

                
                [self.collnew reloadData];
                self.flag1 = [[dic objectForKey:@"data"]objectForKey:@"flag"];
            }path:@"suit_otherNew"];
            
            
            [actionVC.collnew footerEndRefreshing];
        });
    }];
    [self.collAmerica addFooterWithCallback:^{
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            
           
            NSDictionary *dic = @{@"category":@"欧美",
                                  @"lts":@"",
                                  @"pin":@"",
                                  @"flag":self.flag2};
            [AsyncConnection asyncConnection:SUIT_OTHER parmaters:dic block:^(NSData *data) {
                NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
                NSMutableArray *arr = [NSMutableArray array];
                arr = [[dic objectForKey:@"data"]objectForKey:@"items"];
                
                [self.otherArr1 addObjectsFromArray:arr];
                for (int i = 0; i < arr.count; i++) {
                    ItemsModel *model = [[ItemsModel alloc]init];
                    [model setValuesForKeysWithDictionary:[[self.otherArr1 objectAtIndex:i]objectForKey:@"component"]];
                    model.height = [[[self.otherArr1 objectAtIndex:i]objectForKey:@"height"]floatValue];
                    model.width = [[[self.otherArr1 objectAtIndex:i]objectForKey:@"width"]floatValue];
                    model.desc = [[[self.otherArr1 objectAtIndex:i]objectForKey:@"component"]objectForKey:@"description"];
                    model.iid = [[[self.otherArr1 objectAtIndex:i]objectForKey:@"component"]objectForKey:@"id"];
                    [self.collArr addObject:model];
                }
                
                
                [self.collAmerica reloadData];
                self.flag2 = [[dic objectForKey:@"data"]objectForKey:@"flag"];
            }path:@"suit_other_american"];
            
            
            [actionVC.collAmerica footerEndRefreshing];
        });
    }];

    [self.collKorea addFooterWithCallback:^{
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            
            
            NSDictionary *dic = @{@"category":@"日韩",
                                  @"lts":@"",
                                  @"pin":@"",
                                  @"flag":self.flag3};
            [AsyncConnection asyncConnection:SUIT_OTHER parmaters:dic block:^(NSData *data) {
                NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
                NSMutableArray *arr = [NSMutableArray array];
                arr = [[dic objectForKey:@"data"]objectForKey:@"items"];
                
                [self.otherArr2 addObjectsFromArray:arr];
                for (int i = 0; i < arr.count; i++) {
                    ItemsModel *model = [[ItemsModel alloc]init];
                    [model setValuesForKeysWithDictionary:[[self.otherArr2 objectAtIndex:i]objectForKey:@"component"]];
                    model.height = [[[self.otherArr2 objectAtIndex:i]objectForKey:@"height"]floatValue];
                    model.width = [[[self.otherArr2 objectAtIndex:i]objectForKey:@"width"]floatValue];
                    model.desc = [[[self.otherArr2 objectAtIndex:i]objectForKey:@"component"]objectForKey:@"description"];
                    model.iid = [[[self.otherArr2 objectAtIndex:i]objectForKey:@"component"]objectForKey:@"id"];
                    [self.collArr addObject:model];
                }
                
                
                [self.collKorea reloadData];
                self.flag3 = [[dic objectForKey:@"data"]objectForKey:@"flag"];
            }path:@"suit_other_korea"];
            
            
            [actionVC.collKorea footerEndRefreshing];
        });
    }];


    
    
}

- (void)viewWillAppear:(BOOL)animated
{
    
    if (self.otherArr.count != 0) {
        [self.collnew reloadData];
    }
    if (self.otherArr1.count != 0) {
        [self.collAmerica reloadData];
        
    }
    if (self.otherArr2.count != 0) {
        [self.collKorea reloadData];
    }
}
#pragma mark - 
#pragma mark 数据解析

- (void)hotConnection
{
    
    NSDictionary *dic = @{@"token":@"1416b90f03d637a168f695105aa4132d7",
                          @"page":@"1",
                          @"ua":@"iPhone%7C1.7.11",
                          @"lang":@"en",
                          @"sig":@"34fcc5e66a371441154272890",
                          @"nt":@"wifi"};
    [AsyncConnection asyncConnection:SUIT_HOT parmaters:dic block:^(NSData *data) {
        NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
        self.array = [dic objectForKey:@"topics"];
        
        
        [self.table reloadData];
        [_mb show:NO];
    }path:@"suit_hot"];
    

}

- (void)dataConnection
{
    self.flow1 = [[StoreLayout alloc]init];
    
    NSDictionary *dic = @{@"category":[_arr objectAtIndex:1],
                          @"lts":@"",
                          @"pin":@"",
                          @"flag":@""};
    
   [AsyncConnection asyncConnection:SUIT_OTHER parmaters:dic block:^(NSData *data) {
        NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
        self.otherArr = [[dic objectForKey:@"data"]objectForKey:@"items"];
       for (int i = 0; i < _otherArr.count; i++) {
           self.flag1 = [[dic objectForKey:@"data"]objectForKey:@"flag"];
       
       ItemsModel *model = [[ItemsModel alloc]init];
           [model setValuesForKeysWithDictionary:[[self.otherArr objectAtIndex:i]objectForKey:@"component"]];
           model.height = [[[self.otherArr objectAtIndex:i]objectForKey:@"height"]floatValue];
           model.width = [[[self.otherArr objectAtIndex:i]objectForKey:@"width"]floatValue];
           model.desc = [[[self.otherArr objectAtIndex:i]objectForKey:@"component"]objectForKey:@"description"];
           model.iid = [[[self.otherArr objectAtIndex:i]objectForKey:@"component"]objectForKey:@"id"];
           [self.collArr addObject:model];
           
       }
       self.flow1.dicArray = self.otherArr;

       [self.collnew reloadData];
       
    }path:@"other_new"];
    
    [self Aconnection];
}
- (void)Aconnection
{
    self.flow2 = [[StoreLayout alloc]init];
    NSDictionary *dic1 = @{@"category":[_arr objectAtIndex:2],
                          @"lts":@"",
                          @"pin":@"",
                          @"flag":@""};
    [AsyncConnection asyncConnection:SUIT_OTHER parmaters:dic1 block:^(NSData *data) {
        NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
        self.flag2 = [[dic objectForKey:@"data"]objectForKey:@"flag"];
        self.otherArr1 = [[dic objectForKey:@"data"]objectForKey:@"items"];
        
        for (int i = 0; i < _otherArr1.count; i++) {
            
            
            ItemsModel *model = [[ItemsModel alloc]init];
            [model setValuesForKeysWithDictionary:[[self.otherArr1 objectAtIndex:i]objectForKey:@"component"]];
            model.height = [[[self.otherArr1 objectAtIndex:i]objectForKey:@"height"]floatValue];
            model.width = [[[self.otherArr1 objectAtIndex:i]objectForKey:@"width"]floatValue];
            model.desc = [[[self.otherArr1 objectAtIndex:i]objectForKey:@"component"]objectForKey:@"description"];
            model.iid = [[[self.otherArr1 objectAtIndex:i]objectForKey:@"component"]objectForKey:@"id"];
            [self.collArr insertObject:model atIndex:self.otherArr.count + i];
            
        }
        self.flow2.dicArray = self.otherArr1;

        [self.collAmerica reloadData];

        
    }path:@"suit_other_amrecain"];
    [self Kconnection];
}

- (void)Kconnection
{
    self.flow3 = [[StoreLayout alloc]init];
    NSDictionary *dic2 = @{@"category":[_arr objectAtIndex:3],
                          @"lts":@"",
                          @"pin":@"",
                          @"flag":@""};
    [AsyncConnection asyncConnection:SUIT_OTHER parmaters:dic2 block:^(NSData *data) {
        NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
        self.flag3 = [[dic objectForKey:@"data"]objectForKey:@"flag"];
        self.otherArr2 = [[dic objectForKey:@"data"]objectForKey:@"items"];
        
        for (int i = 0; i < _otherArr2.count; i++) {
            
            
            ItemsModel *model = [[ItemsModel alloc]init];
            [model setValuesForKeysWithDictionary:[[self.otherArr2 objectAtIndex:i]objectForKey:@"component"]];
            model.height = [[[self.otherArr2 objectAtIndex:i]objectForKey:@"height"]floatValue];
            model.width = [[[self.otherArr2 objectAtIndex:i]objectForKey:@"width"]floatValue];
            model.desc = [[[self.otherArr2 objectAtIndex:i]objectForKey:@"component"]objectForKey:@"description"];
            model.iid = [[[self.otherArr2 objectAtIndex:i]objectForKey:@"component"]objectForKey:@"id"];
            
            
            [self.collArr insertObject:model atIndex:self.otherArr.count + self.otherArr1.count + i];
            
        }
        self.flow3.dicArray = self.otherArr2;


        [self.collKorea reloadData];

    
            
        
    }path:@"suit_other_korea"];

//    [self addHeader];
//    [self addFooter];

    
}


#pragma mark - 
#pragma mark 上面的scroll

- (void)createScrollView
{
    
    self.scroll = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 64, self.view.frame.size.width - 50*_offWidth, 40 * _offHeight)];
    [self.view addSubview:self.scroll];
    self.scroll.contentSize = CGSizeMake(self.myArr.count * 65*_offWidth, 0);
    self.scroll.delegate = self;
    self.automaticallyAdjustsScrollViewInsets = NO;
    self.scroll.backgroundColor = [UIColor clearColor];
    for (int i = 0; i < self.myArr.count; i++) {
    
        UIButton *button = [[UIButton alloc]initWithFrame:CGRectMake(i * 80*_offWidth + 20*_offWidth, 0, 70*_offWidth, 40*_offHeight)];
        [button setTitle:[_myArr objectAtIndex:i] forState:UIControlStateNormal];
        [button setTitleColor:[UIColor lightGrayColor] forState:UIControlStateNormal];
        [button addTarget:self action:@selector(jump:) forControlEvents:UIControlEventTouchUpInside];
        button.tag = i + 10;
        if ([button.titleLabel.text isEqualToString:@"最新"]) {
            [button setTitleColor:[UIColor colorWithRed:255 / 255 green:0 blue:255 /255 alpha:0.8] forState:UIControlStateNormal];
            button.titleLabel.font = [UIFont boldSystemFontOfSize:20];
        }
        [self.scroll addSubview:button];
        [button release];
    }
    

    [self.scroll release];
    

    
}

- (void)jump:(UIButton *)button
{
    
    self.bigScroll.contentOffset = CGPointMake((button.tag - 10) * self.view.frame.size.width, 0);
    NSArray *arr = self.scroll.subviews;
    for (UIButton *view in arr) {
        
        if ([view isKindOfClass:[UIButton class]]) {
            if (view.tag == button.tag) {
                
                
                [view setTitleColor:[UIColor colorWithRed:255 / 255 green:0 / 255 blue:255 / 255 alpha:1] forState:UIControlStateNormal];
                view.titleLabel.font = [UIFont boldSystemFontOfSize:20];
            }
            else
            {
                [view setTitleColor:[UIColor darkGrayColor] forState:UIControlStateNormal];
                view.titleLabel.font = [UIFont systemFontOfSize:17];
                
            }
        }
    }

}



#pragma mark - 
#pragma mark - 热门的tableView

- (void)createTableView
{
    self.table = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height - 112 - 40*_offHeight) style:UITableViewStylePlain];
    [self.bigScroll addSubview:self.table];
    self.table.delegate = self;
    self.table.dataSource = self;
    [self.table release];
    

//    [self createNaviText];
    
    BOOL net = [StoreViewController judgeConnect];
    if (net) {
        
    
    UIView *view = [[UIView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
    self.mb = [[MBProgressHUD alloc]initWithView:view];
    _mb.center = self.table.center;
    [self.table addSubview:_mb];
    self.mb.labelText = @"努力加载着";
    if (self.array.count == 0) {
        [_mb show:YES];
    }
    }
    
    
}

+ (BOOL)judgeConnect
{
    Reachability *reach = [Reachability reachabilityWithHostName:@"www.baidu.com"];
    NetworkStatus sta = [reach currentReachabilityStatus];
    if (sta == NotReachable) {
        return NO;
    }else{
        return YES;
    }
}

- (void)createLeftTable
{
    self.left = [[UITableView alloc]initWithFrame:CGRectMake(0, 20*_offHeight, self.view.frame.size.width - 50*_offWidth, self.view.frame.size.height - 65*_offHeight)];
    
    [self.view addSubview:_left];
    _left.tag = 27;
    _left.delegate = self;
    _left.dataSource = self;
    
    
    UISwipeGestureRecognizer *swip = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(swip:)];
    [_left addGestureRecognizer:swip];
    
    swip.direction = UISwipeGestureRecognizerDirectionLeft;
    [swip release];
    [_left release];
}



- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (tableView == _left) {
        return self.searchArr.count;
    }else
    {
        
        return self.array.count;
    }
    
}



- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (tableView == self.left) {
        
        
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"re"];
        if (cell == nil) {
            cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"re"];
            
            cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
        }
        cell.textLabel.text = [_searchArr objectAtIndex:indexPath.row];
        cell.backgroundColor = [UIColor clearColor];
        return cell;
    }else
    {
        [_mb removeFromSuperview];
        OneDetailTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"reuse"];
        if (cell == nil) {
            cell = [[OneDetailTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"reuse"];
        }
        [cell.image sd_setImageWithURL:[NSURL URLWithString:[[self.array objectAtIndex:indexPath.row]objectForKey:@"img"]] placeholderImage:[UIImage imageNamed:@"zw.jpg"]];
        cell.title.text = [[self.array objectAtIndex:indexPath.row]objectForKey:@"title"];
        cell.content.text = [[self.array objectAtIndex:indexPath.row]objectForKey:@"desc"];
        
        cell.backgroundColor = [UIColor clearColor];
        return cell;
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (tableView == self.table) {
        return 150 * _offHeight;
    }else
    {
        return 44 * _offHeight;
    }
    
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (tableView == self.left) {
        
        
//        NSArray *arr = @[@"%E8%8C%83%E5%86%B0%E5%86%B0", @"%E6%97%A0%E8%A2%96T%E6%81%A4", @"%E5%B0%96%E5%A4%B4%E9%9E%8B", @"%E7%A7%80%E5%9C%BA", @"%E5%87%89%E9%9E%8B", @"%E8%8C%83%E5%86%B0%E5%86%B0", @"%E7%89%9B%E4%BB%94%E7%9F%AD%E8%A3%A4", @"%E7%A0%B4%E6%B4%9E%E7%89%9B%E4%BB%94%E8%A3%A4", @"%E5%8D%B0%E8%8A%B1%E8%BF%9E%E8%A1%A3%E8%A3%99", @"%E6%89%8B%E6%8F%90%E5%8C%85", @"%E7%9A%AE%E8%A3%A4", @"%E5%80%AA%E5%A6%AE", @"%E6%9E%97%E7%8F%8D%E5%A8%9C",  @"%E7%B4%A7%E8%BA%AB%E7%89%9B%E4%BB%94%E8%A3%A4",  @"%E5%94%87",  @"%E6%9D%A1%E7%BA%B9",  @"%E7%89%9B%E4%BB%94",  @"%E5%8D%95%E8%82%A9%E5%8C%85",  @"%E5%8F%8C%E8%82%A9%E5%8C%85"];
        self.navigationController.navigationBar.hidden = NO;
        [[self.view viewWithTag:27]removeFromSuperview];
    }
    if (tableView == self.table) {
        TopicDetailViewController *detail = [[TopicDetailViewController alloc]init];
        detail.hidesBottomBarWhenPushed = YES;
        detail.tid = [[self.array objectAtIndex:indexPath.row]objectForKey:@"topicId"];
        [self.navigationController pushViewController:detail animated:YES];
        [detail release];
    }
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    if (tableView == self.left) {
        
        
        static NSString *cellSection = @"section";
        HeaderFooterView *my = [tableView dequeueReusableCellWithIdentifier:cellSection];
        if (!my) {
            my = [[[HeaderFooterView alloc]initWithReuseIdentifier:cellSection]autorelease];
           
        }
        
        if (section == 0) {
            my.title.text = @"最近热门搜索";
        }
        return my;
    }else
    {
        return [[[UIView alloc]init]autorelease];
    }
    
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    if (tableView == self.left) {
        return 40*_offHeight;
    }else
    {
        return 0;
    }
    
}

- (void)swip:(UISwipeGestureRecognizer *)swip
{
    [[self.view viewWithTag:27]removeFromSuperview];
    self.navigationController.navigationBar.hidden = NO;
    self.bigScroll.userInteractionEnabled = YES;
}




#pragma mark -
#pragma mark - 收藏
- (void)shoucang:(UIButton *)button
{
   
    [DataBase dataBaseOpen];
    
    if (button.selected == YES) {
        [DataBase dataBaseOpen];
        ItemsModel *model = [[ItemsModel alloc]init];
        model = [self.collArr objectAtIndex:button.tag];
        [ItemDataBase deleteWithid:model.iid];
        NSInteger i = [model.collectionCount integerValue];
        [button setTintColor:[UIColor darkGrayColor]];
        [button setImage:[UIImage imageNamed:@"heart.png"] forState:UIControlStateNormal];
       
        [button setTitle:[NSString stringWithFormat:@"%ld", i] forState:UIControlStateNormal];
        
    }
    if (button.selected == NO) {
        ItemsModel *model = [self.collArr objectAtIndex:button.tag];
        
        [ItemDataBase insertWithModel:model];
        
        [button setImage:[UIImage imageNamed:@"sheart.png"] forState:UIControlStateNormal];
        [button setTintColor:[UIColor clearColor]];
        NSInteger i = [model.collectionCount integerValue];
        
        i = i + 1;
        
        
        [button setTitle:[NSString stringWithFormat:@"%ld", i] forState:UIControlStateNormal];
        
    }
    if (button.selected == YES) {
        button.selected = NO;
    }else
    {
        button.selected = YES;
    }
    
}
#pragma mark -
#pragma mark- 其他页的collectionview
- (void)createNewCollection
{
        self.collnew = [[UICollectionView alloc]initWithFrame:CGRectMake(self.bigScroll.frame.size.width, 0, self.view.frame.size.width, self.view.frame.size.height - 112 - 40*_offHeight) collectionViewLayout:_flow1];

    
        self.collnew.backgroundColor = [UIColor colorWithRed:0.1 green:0.1 blue:.1 alpha:0.1];
    
        self.collnew.delegate = self;
        self.collnew.dataSource = self;
        [self.collnew registerClass:[SuitCell class] forCellWithReuseIdentifier:@"aaaa"];
        [self.bigScroll addSubview:self.collnew];
    
}

- (void)createAcollection
{
    self.collAmerica = [[UICollectionView alloc]initWithFrame:CGRectMake(self.bigScroll.frame.size.width * 2 , 0, self.view.frame.size.width, self.view.frame.size.height - 112 - 40*_offHeight) collectionViewLayout:_flow2];
    
    
    self.collAmerica.backgroundColor = [UIColor colorWithRed:0.1 green:0.1 blue:.1 alpha:0.1];
    
    self.collAmerica.delegate = self;
    self.collAmerica.dataSource = self;
    [self.collAmerica registerClass:[SuitCell class] forCellWithReuseIdentifier:@"aaaa"];
    [self.bigScroll addSubview:self.collAmerica];
}

- (void)createKcollection
{
   
    self.collKorea = [[UICollectionView alloc]initWithFrame:CGRectMake(self.bigScroll.frame.size.width * 3, 0, self.view.frame.size.width, self.view.frame.size.height - 112 - 40*_offHeight) collectionViewLayout:_flow3];
    
    self.collKorea.backgroundColor = [UIColor colorWithRed:0.1 green:0.1 blue:.1 alpha:0.1];
    
    self.collKorea.delegate = self;
    self.collKorea.dataSource = self;
    [self.collKorea registerClass:[SuitCell class] forCellWithReuseIdentifier:@"aaaa"];
    [self.bigScroll addSubview:self.collKorea];
    [_collKorea release];
    
}


- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    if (collectionView == self.collnew) {
        return self.otherArr.count;
    }
    if (collectionView == self.collAmerica) {
        return self.otherArr1.count;
    }
    else{
        return self.otherArr2.count;
    }
}


- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    SuitCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"aaaa" forIndexPath:indexPath];
    NSDictionary *dic = [NSDictionary dictionary];
    if (collectionView == self.collnew) {
        dic = [[self.otherArr objectAtIndex:indexPath.item]objectForKey:@"component"];
        cell.like.tag = indexPath.item + self.otherArr.count + self.otherArr1.count + 1;
    }else
    {
    if (collectionView == self.collAmerica) {
        dic = [[self.otherArr1 objectAtIndex:indexPath.item]objectForKey:@"component"];
        cell.like.tag = indexPath.item;
    }
    else{
        dic = [[self.otherArr2 objectAtIndex:indexPath.item]objectForKey:@"component"];
        cell.like.tag = indexPath.item  + self.otherArr1.count;
    }
    }
    
//    SuitCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"aaaa" forIndexPath:indexPath];
    cell.backgroundColor = [UIColor clearColor];
    NSString *pic = [[[dic objectForKey:@"picUrl"]componentsSeparatedByString:@"?"]objectAtIndex:0];
    [cell.image sd_setImageWithURL:[NSURL URLWithString:pic] placeholderImage:[UIImage imageNamed:@"zw.jpg"]];
    cell.title.text = [dic objectForKey:@"description"];
    [cell.share setTitle:[dic objectForKey:@"itemsCount"] forState:UIControlStateNormal];
    NSInteger i = [[dic objectForKey:@"collectionCount"] integerValue];
    
    
    BOOL exit = [ItemDataBase selectWithid:[dic objectForKey:@"id"]];
    if (exit) {
        cell.like.selected = YES;
        [cell.like setImage:[UIImage imageNamed:@"sheart.png"] forState:UIControlStateNormal];
        [cell.like setTitle:[NSString stringWithFormat:@"%ld", i + 1] forState:UIControlStateNormal];
        cell.like.tintColor = [UIColor colorWithRed:255/255 green:0 blue:255/255 alpha:0.8];
    }else
    {
        [cell.like setImage:[UIImage imageNamed:@"heart.png"] forState:UIControlStateNormal];
        [cell.like setTintColor:[UIColor darkGrayColor]];
        [cell.like setTitle:[NSString stringWithFormat:@"%ld", i] forState:UIControlStateNormal];
        cell.like.selected = NO;
    }
//    cell.like.tag = indexPath.item;
    [cell.like addTarget:self action:@selector(shoucang:) forControlEvents:UIControlEventTouchUpInside];
    cell.layer.cornerRadius = 5;
    cell.layer.masksToBounds = YES;
    cell.backgroundColor = [UIColor whiteColor];
    return cell;
    
    
}


- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    NSDictionary *dic = [NSDictionary dictionary];
    if (collectionView == self.collnew) {
        dic = [self.otherArr objectAtIndex:indexPath.item];
    }
    if (collectionView == self.collAmerica) {
        dic = [self.otherArr1 objectAtIndex:indexPath.item];
    }
    if (collectionView == self.collKorea) {
        dic = [self.otherArr2 objectAtIndex:indexPath.item];
    }
    
    CollectionController *coll = [[CollectionController alloc]init];
    coll.aid = [[[dic objectForKey:@"component"]objectForKey:@"action"]objectForKey:@"id"];
    coll.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:coll animated:YES];
    [coll release];
    
}






#pragma mark -
#pragma mark 创建大的scrollview
- (void)createBigScroll
{
    self.bigScroll = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 64 + 40 *_offHeight, self.view.frame.size.width, self.view.frame.size.height - 112 - 40 *_offHeight)];
    [self.view addSubview:_bigScroll];

    _bigScroll.contentSize = CGSizeMake(self.myArr.count * self.view.frame.size.width, 0);
    _bigScroll.delegate = self;
    _bigScroll.pagingEnabled = YES;
    _bigScroll.bounces = NO;
    [_bigScroll release];
    _bigScroll.backgroundColor = [UIColor clearColor];
    
    [self createTableView];
  
}

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    
    if (scrollView == self.bigScroll) {
        
    
            NSArray *array = self.scroll.subviews;
            for (UIButton *button in array) {
                if ([button isKindOfClass:[UIButton class]]) {
                    if (button.tag == self.bigScroll.contentOffset.x / self.view.frame.size.width + 10) {
                        button.titleLabel.font = [UIFont boldSystemFontOfSize:20];
                        [button setTitleColor:[UIColor colorWithRed:255/255 green:0 blue:255/255 alpha:0.8] forState:UIControlStateNormal];
                    }else
                    {
                        button.titleLabel.font = [UIFont systemFontOfSize:17];
                        [button setTitleColor:[UIColor darkGrayColor] forState:UIControlStateNormal];
                    }
                }
            }
        }
    


}

- (void)scrollViewWillBeginDecelerating:(UIScrollView *)scrollView
{
    if (scrollView == self.bigScroll) {

    if (scrollView.contentOffset.x <= 0) {
        [self createLeftTable];
        self.navigationController.navigationBar.hidden = YES;
    }
    }
}




// headerView


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
